package com.superteam.zzl.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.superteam.zzl.activity.R;
import com.superteam.zzl.activity.databinding.ActivityAuthorFootBinding;
import com.superteam.zzl.activity.databinding.ActivityAuthorItemBinding;
import com.superteam.zzl.model.DynamicFragment.bean.Dynamic;
import com.superteam.zzl.utils.DisplayUtils;
import com.superteam.zzl.viewmodel.DynamicFragment.ContentInfoPresenter;

import java.util.List;

/**
 * Created by Administrator on 2017/1/11.
 */

public class AuthorActivityAdapter extends RecyclerViewBaseAdapter<Dynamic.DataBean.TrendsListBean.ImageListBean, AuthorActivityAdapter.ViewHolde> {

    private  ContentInfoPresenter presenter;
    private LayoutInflater inflater;

    public AuthorActivityAdapter(Context context, List<Dynamic.DataBean.TrendsListBean.ImageListBean> list, ContentInfoPresenter contentInfoPresenter) {
        super(context, list);
        inflater = LayoutInflater.from(context);
        this.presenter = contentInfoPresenter;
    }

    @Override
    public int getItemViewType(int position) {
        int i = getItemCount() - 1;
        return position == i ? 1 : 0;
    }




    @Override
    public ViewHolde onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == 0) {
            view = inflater.inflate(R.layout.activity_author_item, parent, false);
        } else {
            view = inflater.inflate(R.layout.activity_author_foot, parent, false);
        }
        return new ViewHolde(view, viewType);
    }

    @Override
    public int getItemCount() {
        if (list.size() == 0 || list == null) {
            return 0;
        } else {
            return list.size() + 1;
        }
    }

    @Override
    public void onBindViewHolder(ViewHolde holder, int position) {
        super.onBindViewHolder(holder, position);
        if (getItemViewType(position) == 0) {
            if (holder.viewType == 1) {
                holder = onCreateViewHolder(presenter.getRecyclerView(), 0);
            }
        } else {
            if (holder.viewType == 0) {
                holder = onCreateViewHolder(presenter.getRecyclerView(), 1);
            }
        }
        Dynamic.DataBean.TrendsListBean trends = presenter.getData();
        if (holder.viewType == 0) {
            ActivityAuthorItemBinding binding = holder.getBinding();
            binding.setEvent(presenter.getActivity());
            binding.iv.setPosition(position);
            Glide.with(context).load(list.get(position).image_url).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.ALL).into(binding.iv);
            binding.executePendingBindings();
        } else {
            holder.getFdb().setTrends(trends);
            holder.getFdb().rb.setText("赞(" + trends.fav_count + ")");
            ViewGroup.LayoutParams layoutParams = holder.getFdb().lv.getLayoutParams();
            if (trends.comment_count != null && trends.comment_count.length() > 0 && Integer.parseInt(trends.comment_count) > 0) {
                holder.getFdb().commentContent.setVisibility(View.VISIBLE);
                AuthorActivityFootAdapter adapter = new AuthorActivityFootAdapter(context, trends.comment_list, R.layout.activity_author_foot_lv);
                holder.adapter = adapter;
                int i = Integer.parseInt(trends.comment_count);
                if (i == 1) {
                    layoutParams.height = DisplayUtils.dipTopx(60 * i);
                } else {
                    layoutParams.height = DisplayUtils.dipTopx(61 * (i - 1) + 60);
                }
                holder.getFdb().lv.setAdapter(adapter);
            } else {
                holder.getFdb().commentContent.setVisibility(View.GONE);
                layoutParams.height = 0;
            }

            holder.getFdb().executePendingBindings();

        }

    }

    class ViewHolde extends RecyclerView.ViewHolder {

        public int viewType;
        public AuthorActivityFootAdapter adapter;
        private ActivityAuthorItemBinding db;
        private ActivityAuthorFootBinding fdb;

        public ViewHolde(View itemView, int viewType) {
            super(itemView);
            this.viewType = viewType;
            if (viewType == 0) {
                db = DataBindingUtil.bind(itemView);
            } else {
                fdb = DataBindingUtil.bind(itemView);
            }

        }

        public ActivityAuthorItemBinding getBinding() {
            return db;
        }

        public ActivityAuthorFootBinding getFdb() {
            return fdb;
        }
    }


}
